package com.kanshu.ksgb.fastread.doudou.common.business.ad;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.text.TextUtils;
import android.util.Log;

import com.kanshu.ksgb.fastread.doudou.app.constants.ARouterConfig;
import com.kanshu.ksgb.fastread.doudou.common.business.ad.dialog.AdHotSplashDialog;
import com.kanshu.ksgb.fastread.doudou.common.business.ad.retrofit.ADConfigBean;
import com.kanshu.ksgb.fastread.doudou.common.business.ad.retrofit.ADConfigs;
import com.kanshu.ksgb.fastread.doudou.common.util.ActivityMgr;

import java.util.concurrent.TimeUnit;

import io.reactivex.android.schedulers.AndroidSchedulers;

/**
 * Title:
 * Description:
 * Copyright: Copyright(c)2018
 * Company: 看书网
 * CreateTime: 2018/4/24 17:43
 *
 * @author EvanHe
 * @version 1.0
 */
public class LifecycleHelper {
    // 广告显示间隔时间
    private long mADInterval = 1000 * 6 * 15;//3.0.7产品需求修复时间(当前时间为1分30秒)
    //    private long mADInterval = 1000 * 3;//测试
    private static boolean isClickHome = false;
    private static boolean isRecentApps = false;
    // 上次显示广告的时间
    private static long mLastADTime = 0;

    private AdHotSplashDialog mDialog;

    private BroadcastReceiver mHomeKeyEventReceiver = new BroadcastReceiver() {
        String SYSTEM_REASON = "reason";
        String SYSTEM_HOME_KEY = "homekey";
        String SYSTEM_HOME_KEY_LONG = "recentapps";

        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if (action.equals(Intent.ACTION_CLOSE_SYSTEM_DIALOGS)) {
                String reason = intent.getStringExtra(SYSTEM_REASON);
                if (TextUtils.equals(reason, SYSTEM_HOME_KEY)) {
                    isClickHome = true;
                    if (!isRecentApps) {
                        mLastADTime = System.currentTimeMillis();
                    }
                }
            }
        }
    };

    public void onActivityCreated(Activity activity) {
        if (!TextUtils.equals(ARouterConfig.PageTagParams.SPLASH_PAGE, ActivityMgr.getInstance().getActivityTag(activity)) && existTimeSplashAd()) {
            try {
                activity.registerReceiver(mHomeKeyEventReceiver, new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS));
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    public void onActivityStarted(Activity activity) {

    }

    public void onActivityResumed(Activity activity) {
        boolean b = isClickHome && canShowAD() && mLastADTime > 0 && existTimeSplashAd();
        Log.e("evan", "mLastADTime：" + mLastADTime + "   isClickHome：" + isClickHome + "   canShowAD：" + canShowAD());
        if (b) {
            AndroidSchedulers.mainThread().scheduleDirect(() -> {
                if (activity != null && !activity.isFinishing()) {
                    mDialog = new AdHotSplashDialog(activity);
                    mDialog.show();
                }
            }, 200, TimeUnit.MILLISECONDS);
        }
        isClickHome = false;
        mLastADTime = 0;
        isRecentApps = false;
    }


    public void onActivityPaused(Activity activity) {
        isRecentApps = true;
    }

    public void onActivityStopped(Activity activity) {
        if (mDialog != null && mDialog.isShowing()) {
            mDialog.dismiss();
            mDialog = null;
        }
    }


    public void onActivitySaveInstanceState(Activity activity) {

    }

    public void onActivityDestroyed(Activity activity) {
        if (activity != null && !TextUtils.equals(ARouterConfig.PageTagParams.SPLASH_PAGE, ActivityMgr.getInstance().getActivityTag(activity)) && existTimeSplashAd()) {
            try {
                activity.unregisterReceiver(mHomeKeyEventReceiver);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        if (mDialog != null && mDialog.isShowing()) {
            mDialog.dismiss();
            mDialog = null;
        }
    }

    private boolean canShowAD() {
        return System.currentTimeMillis() - mLastADTime > mADInterval;
    }

    /**
     * 是否存在这个闪屏广告
     * @return
     */
    private boolean existTimeSplashAd() {
        return ADConfigs.showAD(String.valueOf(ADConfigBean.POSITION_SPACE_TIME_SPLASH)) != null;
    }
}
